package com.zzyl.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void createBed(BedDto bedDto) {
         Bed  bed = new Bed();
        BeanUtils.copyProperties(bedDto,bed);
        bed.setBedStatus(0);
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        try {
            bedMapper.createBed(bed);
        }catch(Exception e){
            throw new BaseException(BasicEnum.BED_INSERT_FAILED);
        }
    }

    @Override
    public BedVo getIdselectBed(Integer id) {
        BedVo idselectBed = bedMapper.getIdselectBed(id);
           return  idselectBed;
    }

    @Override
    public void updateBed(BedDto bedDto) {
         Bed bed=new Bed();
         BeanUtils.copyProperties(bedDto,bed);
         bed.setUpdateTime(LocalDateTime.now());
        try{
            bedMapper.updateBed(bed);
        }catch (Exception e){
            throw new BaseException(BasicEnum.BED_INSERT_FAILED);
        }
    }

    @Override
    public void DeleteBed(long id) {
        bedMapper.DeleteBed(id);
    }

}

